package android.serialport;

import org.json.JSONArray;

import java.util.Date;

public class SerialPortConstants {
    public static JSONArray ADVERT;
    public static JSONArray ADVERT_VIDEOS;
    public static String CHAT_SERVER_URL = "http://www.cqlggc.com";
    public static String[] COLORS;
    public static boolean DEBUG = false;
    public static int LAST_TIME = 0;
    public static String LOG;
    public static String QRCODE;
    public static final String SERVER_HOST = "http://www.cqlggc.com";


    public static final byte[] GETSN;
    public static final byte[] HEART;
    public static final byte[] RESET;
    public static final byte[] SELECT_RET;
    public static final byte[] SELECT_CET;
    public static final byte[] SELECT_CHAO;
    public static final byte[] SELECT_TIE;
    public static final byte[] RET_RESET;
    public static final byte[] RET_BIG;
    public static final byte[] RET_MIDDLE;
    public static final byte[] RET_SMALL;
    public static final byte[] RET_MOD_DEC;
    public static final byte[] RET_MOD_ADD;
    public static final byte[] RET_ENERGY_DEC;
    public static final byte[] RET_ENERGY_ADD;
    public static final byte[] RET_START;
    public static final byte[] RET_END;
    public static final byte[] CET_RESET;
    public static final byte[] CET_BIG;
    public static final byte[] CET_MIDDLE;
    public static final byte[] CET_SMALL;
    public static final byte[] CET_MOD_DEC;
    public static final byte[] CET_MOD_ADD;
    public static final byte[] CET_ENERGY_DEC;
    public static final byte[] CET_ENERGY_ADD;
    public static final byte[] CET_START;
    public static final byte[] CET_END;
    public static final byte[] CHAO_RESET;
    public static final byte[] CHAO_MOD_DEC;
    public static final byte[] CHAO_MOD_ADD;
    public static final byte[] CHAO_ENERGY_DEC;
    public static final byte[] CHAO_ENERGY_ADD;
    public static final byte[] CHAO_START;
    public static final byte[] CHAO_END;
    public static final byte[] TIE_RESET;
    public static final byte[] TIE_MOD_DEC;
    public static final byte[] TIE_MOD_ADD;
    public static final byte[] TIE_ENERGY_DEC;
    public static final byte[] TIE_ENERGY_ADD;
    public static final byte[] TIE_ENERGY_DEC1;
    public static final byte[] TIE_ENERGY_ADD1;
    public static final byte[] TIE_START;
    public static final byte[] TIE_END;
    public static String SN;

    public static String matters_link = "";


    static {

        //读取SN编号
        GETSN = new byte[]{123, -73, 3, 1, hexToByte("D6"), hexToByte("69")};
        //心跳
        HEART = new byte[]{123, -73, 3, 2, hexToByte("D7"), hexToByte("29")};
        //恢复初始化状态
        RESET = new byte[]{123, -73, 3, 3, hexToByte("17"), hexToByte("E8")};
        //选择RET功能
        SELECT_RET = new byte[]{123, -73, 3, 4, hexToByte("D5"), hexToByte("A9")};

        //选择CET功能
        SELECT_CET = new byte[]{123, -73, 3, 5, hexToByte("15"), hexToByte("68")};


        //RET参数复位
        RET_RESET = new byte[]{123, -73, 3, 6, hexToByte("14"), hexToByte("28")};
        //选择RET大手柄
        RET_BIG = new byte[]{123, -73, 3, 7, hexToByte("D4"), hexToByte("E9")};
        //选择RET中手柄
        RET_MIDDLE = new byte[]{123, -73, 3, 8, hexToByte("D0"), hexToByte("A9")};
        //RET模式减
        RET_MOD_DEC = new byte[]{123, -73, 3, 9, hexToByte("10"), hexToByte("68")};
        //RET模式加
        RET_MOD_ADD = new byte[]{123, -73, 3, 10, hexToByte("11"), hexToByte("28")};
        //RET能量减
        RET_ENERGY_DEC = new byte[]{123, -73, 3, 11, hexToByte("D1"), hexToByte("E9")};
        //RET能量加
        RET_ENERGY_ADD = new byte[]{123, -73, 3, 12, hexToByte("13"), hexToByte("A8")};
        //RET开关
        RET_START = new byte[]{123, -73, 3, 13, hexToByte("D3"), hexToByte("69")};


        //CET参数复位
        RET_END = new byte[]{123, -73, 3, 14, hexToByte("D2"), hexToByte("29")};
        //选择CET大手柄
        CET_RESET = new byte[]{123, -73, 3, 15, hexToByte("12"), hexToByte("E8")};
        //选择CET中手柄
        CET_BIG = new byte[]{123, -73, 3, 16, hexToByte("DA"), hexToByte("A9")};
        //选择CET小手柄
        CET_MIDDLE = new byte[]{123, -73, 3, 17, hexToByte("1A"), hexToByte("68")};
        //RET模式减
        CET_SMALL = new byte[]{123, -73, 3, 18, hexToByte("1B"), hexToByte("28")};
        //RET模式加
        CET_MOD_DEC = new byte[]{123, -73, 3, 19, hexToByte("DB"), hexToByte("E9")};
        //RET能量减
        CET_MOD_ADD = new byte[]{123, -73, 3, 20, hexToByte("19"), hexToByte("A8")};
        //RET能量加
        CET_ENERGY_DEC = new byte[]{123, -73, 3, 21, hexToByte("D9"), hexToByte("69")};
        //CET开关
        CET_ENERGY_ADD = new byte[]{123, -73, 3, 22, hexToByte("D8"), hexToByte("29")};

        CET_START = new byte[]{123, -73, 3, 23, hexToByte("18"), hexToByte("E8")};

        CET_END = new byte[]{123, -73, 3, 24, hexToByte("1C"), hexToByte("A8")};

        RET_SMALL = new byte[]{123, -73, 3, 25, hexToByte("DC"), hexToByte("69")};
        //选择CHAO功能
        SELECT_CHAO = new byte[]{123, -73, 3, 26, hexToByte("DD"), hexToByte("29")};
        //选择TIE功能
        SELECT_TIE = new byte[]{123, -73, 3, 27, hexToByte("1D"), hexToByte("E8")};
        //超声波参数复位
        CHAO_RESET = new byte[]{123, -73, 3, 28, hexToByte("DF"), hexToByte("A9")};
        //超声波模式减
        //超声波模式加
        CHAO_MOD_DEC = new byte[]{123, -73, 3, 29, hexToByte("1F"), hexToByte("68")};
        //超声波能量减
        CHAO_MOD_ADD = new byte[]{123, -73, 3, 30, hexToByte("1E"), hexToByte("28")};
        //超声波能量加
        CHAO_ENERGY_DEC = new byte[]{123, -73, 3, 31, hexToByte("DE"), hexToByte("E9")};
        //超声波开关
        CHAO_ENERGY_ADD = new byte[]{123, -73, 3, 32, hexToByte("CE"), hexToByte("A9")};

        CHAO_START = new byte[]{123, -73, 3, 33, hexToByte("0E"), hexToByte("68")};

        CHAO_END = new byte[]{123, -73, 3, 34, hexToByte("0F"), hexToByte("28")};

        //贴参数复位
        TIE_RESET = new byte[]{123, -73, 3, 35, hexToByte("CF"), hexToByte("E9")};
        //贴模式减
        //贴模式加
        TIE_MOD_DEC = new byte[]{123, -73, 3, 36, hexToByte("0D"), hexToByte("A8")};
        //贴能量减
        TIE_MOD_ADD = new byte[]{123, -73, 3, 37, hexToByte("CD"), hexToByte("69")};
        //贴能量加
        TIE_ENERGY_DEC = new byte[]{123, -73, 3, 38, hexToByte("CC"), hexToByte("29")};
        //贴开关
        TIE_ENERGY_ADD = new byte[]{123, -73, 3, 39, hexToByte("0C"), hexToByte("E8")};
        //贴能量加
        TIE_ENERGY_DEC1 = new byte[]{123, -73, 3, 40, hexToByte("08"), hexToByte("A8")};
        //贴开关
        TIE_ENERGY_ADD1 = new byte[]{123, -73, 3, 41, hexToByte("C8"), hexToByte("69")};
        TIE_START = new byte[]{123, -73, 3, 42, hexToByte("C9"), hexToByte("29")};

        TIE_END = new byte[]{123, -73, 3, 43, hexToByte("09"), hexToByte("E8")};
        SerialPortConstants.LOG = "";
        SerialPortConstants.DEBUG = false;
    }

    /**
     * Hex字符串转byte
     *
     * @param inHex 待转换的Hex字符串
     * @return 转换后的byte
     */
    public static byte hexToByte(String inHex) {
        return (byte) Integer.parseInt(inHex, 16);
    }

    public static String byteArrayToHex(byte[] bytes) {
        StringBuilder result = new StringBuilder();
        for (int index = 0, len = bytes.length; index <= len - 1; index += 1) {
            int char1 = ((bytes[index] >> 4) & 0xF);
            char chara1 = Character.forDigit(char1, 16);
            int char2 = ((bytes[index]) & 0xF);
            char chara2 = Character.forDigit(char2, 16);
            result.append(chara1);
            result.append(chara2);
        }
        return result.toString();
    }

    public static String byteArrayToHexArray(byte[] bytes) {
        StringBuilder result = new StringBuilder();
        for (int index = 0, len = bytes.length; index <= len - 1; index += 1) {
            int char1 = ((bytes[index] >> 4) & 0xF);
            char chara1 = Character.forDigit(char1, 16);
            int char2 = ((bytes[index]) & 0xF);
            char chara2 = Character.forDigit(char2, 16);
            result.append(chara1);
            result.append(chara2);
        }
        return result.toString();
    }

    public static String byteArrayToHex2(byte[] bytes) {
        StringBuilder result = new StringBuilder();
        for (int index = 0, len = bytes.length; index <= len - 1; index += 1) {

            String invalue1 = Integer.toHexString((bytes[index] >> 4) & 0xF);
            String intValue2 = Integer.toHexString(bytes[index] & 0xF);
            result.append(invalue1);
            result.append(intValue2);
        }
        return result.toString();
    }


    public static String byteArrayToString(byte[] bytes) {
        return new String(bytes);
    }


    public static int getSecondTimestamp() {
        final String value = String.valueOf(new Date().getTime());
        final int length = value.length();
        if (length > 3) {
            return Integer.valueOf(value.substring(0, length - 3));
        }
        return 0;
    }
}
